/**
 * Copyright (C) 2019 Baidu, Inc. All Rights Reserved.
 */
package com.bailiban.dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bailiban.dao.OrderDao;
import com.bailiban.pojo.Order;
import com.bailiban.pojo.Stadia;
import com.bailiban.pojo.Tickets;
import com.bailiban.pojo.User;
import com.bailiban.utils.JDBCUtils;
import com.bailiban.utils.ResultMsg;

/**
 * @author zhulang
 * @version 创建时间：2019年9月27日 下午3:32:07
 */
public class OrderDaoImpl implements OrderDao {
    private static Connection connection = null;
    private static PreparedStatement statement = null;
    private static ResultSet rs = null;

    /*
     * (non-Javadoc)
     * 
     * @see com.bailiban.dao.OrderDao#findAllOrder()
     */
    @Override
    public List<Order> findAllOrder() {
        List<Order> list = new ArrayList<Order>();
        try {
            connection = JDBCUtils.getConnection();
            String sql =
                    "SELECT t.tid,t.TNAME,t.beginTime,t.orderCount,t.totalCount,t.price,s.sname,o.orderTime,o.oid,u.`NAME`,u.idCard from tb_tickets t,tb_stadia s,tb_order o,tb_user u WHERE t.s_t_id=s.sid AND o.uid=u.id AND o.tid=t.tid";
            statement = connection.prepareStatement(sql);
            rs = statement.executeQuery();
            while (rs.next()) {
                // 给订单赋值
                Order order = new Order();
                order.setOrderTime(rs.getDate("orderTime"));
                order.setOid(rs.getString("oid"));
                // 给订单里面的门票赋值
                Tickets tickets = new Tickets();
                tickets.setTid(rs.getInt("tid"));
                tickets.setTname(rs.getString("tname"));
                tickets.setBeginTime(rs.getDate("beginTime"));
                tickets.setOrderCount(rs.getInt("orderCount"));
                tickets.setTotalCount(rs.getInt("totalCount"));
                tickets.setPrice(rs.getDouble("price"));
                // 给订单里面的场馆赋值
                Stadia stadia = new Stadia();
                stadia.setSname(rs.getString("sname"));
                // 给订单里面的用户赋值
                User user = new User();
                user.setName(rs.getString("name"));
                user.setIdCard(rs.getString("idCard"));
                // 把场馆、门票、用户赋值给这个订单
                order.setStadia(stadia);
                order.setTickets(tickets);
                order.setUser(user);

                list.add(order);
            }
            JDBCUtils.close(connection, statement, rs);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return list;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.bailiban.dao.OrderDao#findOrderByOid(int)
     */
    @Override
    public List<Order> findOrderByOid(String oid) {
        List<Order> list = new ArrayList<Order>();
        try {
            connection = JDBCUtils.getConnection();
            String sql =
                    "SELECT t.tid,t.TNAME,t.beginTime,t.totalCount,t.orderCount,t.price,s.sname,o.orderTime,o.oid,u.`NAME`,u.idCard from tb_tickets t,tb_stadia s,tb_order o,tb_user u WHERE t.s_t_id=s.sid AND o.uid=u.id AND o.tid=t.tid and o.oid=?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, oid);
            rs = statement.executeQuery();
            while (rs.next()) {
                // 给订单赋值
                Order order = new Order();
                order.setOrderTime(rs.getDate("orderTime"));
                order.setOid(rs.getString("oid"));
                // 给订单里面的门票赋值
                Tickets tickets = new Tickets();
                tickets.setTid(rs.getInt("tid"));
                tickets.setTname(rs.getString("tname"));
                tickets.setBeginTime(rs.getDate("beginTime"));
                tickets.setOrderCount(rs.getInt("orderCount"));
                tickets.setTotalCount(rs.getInt("totalCount"));
                tickets.setPrice(rs.getDouble("price"));
                // 给订单里面的场馆赋值
                Stadia stadia = new Stadia();
                stadia.setSname(rs.getString("sname"));
                // 给订单里面的用户赋值
                User user = new User();
                user.setName(rs.getString("name"));
                user.setIdCard(rs.getString("idCard"));
                // 把场馆、门票、用户赋值给这个订单
                order.setStadia(stadia);
                order.setTickets(tickets);
                order.setUser(user);
                list.add(order);
            }
            JDBCUtils.close(connection, statement, rs);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return list;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.bailiban.dao.OrderDao#findOrderByUid(int)
     */
    @Override
    public List<Order> findOrderByUid(int uid) {
        List<Order> list = new ArrayList<Order>();
        try {
            connection = JDBCUtils.getConnection();
            String sql =
                    "SELECT t.tid,t.TNAME,t.beginTime,t.totalCount,t.orderCount,t.price,s.sname,o.orderTime,o.oid,u.`NAME`,u.idCard from tb_tickets t,tb_stadia s,tb_order o,tb_user u WHERE t.s_t_id=s.sid AND o.uid=u.id AND o.tid=t.tid and o.uid=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, uid);
            rs = statement.executeQuery();
            while (rs.next()) {
                // 给订单赋值
                Order order = new Order();
                order.setOrderTime(rs.getDate("orderTime"));
                order.setOid(rs.getString("oid"));
                // 给订单里面的门票赋值
                Tickets tickets = new Tickets();
                tickets.setTid(rs.getInt("tid"));
                tickets.setTname(rs.getString("tname"));
                tickets.setBeginTime(rs.getDate("beginTime"));
                tickets.setOrderCount(rs.getInt("orderCount"));
                tickets.setTotalCount(rs.getInt("totalCount"));
                tickets.setPrice(rs.getDouble("price"));
                // 给订单里面的场馆赋值
                Stadia stadia = new Stadia();
                stadia.setSname(rs.getString("sname"));
                // 给订单里面的用户赋值
                User user = new User();
                user.setName(rs.getString("name"));
                user.setIdCard(rs.getString("idCard"));
                // 把场馆、门票、用户赋值给这个订单
                order.setStadia(stadia);
                order.setTickets(tickets);
                order.setUser(user);
                list.add(order);
            }
            JDBCUtils.close(connection, statement, rs);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return list;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.bailiban.dao.OrderDao#addOrder(com.bailiban.pojo.Order)
     */
    @Override
    public ResultMsg addOrder(Order order) {
        ResultMsg result = new ResultMsg();
        try {
            connection = JDBCUtils.getConnection();
            String sql = "insert into tb_order(oid,orderTime,uid,tid) values(?,?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, order.getOid());
            statement.setDate(2, new Date(order.getOrderTime().getTime()));
            statement.setInt(3, order.getUid());
            statement.setInt(4, order.getTid());
            int update = statement.executeUpdate();
            if (update > 0) {
                result.setFlag(true);
            } else {
                result.setFlag(false);
            }
            JDBCUtils.close(connection, statement, rs);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return result;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.bailiban.dao.OrderDao#deleteOrderByOid(int)
     */
    @Override
    public ResultMsg deleteOrderByOid(String oid) {
        ResultMsg result = new ResultMsg();
        try {
            connection = JDBCUtils.getConnection();
            String sql = "DELETE from tb_order WHERE oid=?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, oid);
            int update = statement.executeUpdate();
            if (update > 0) {
                result.setFlag(true);
            } else {
                result.setFlag(false);
            }
            JDBCUtils.close(connection, statement, rs);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return result;
    }

}
